﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using THANHTUNGO.PORTAL.Components.Configuration;
using THANHTUNGO.PORTAL.Components.Configuration.Items;
using THANHTUNGO.PORTAL.Components.Security;
using THANHTUNGO.PORTAL.Components.UI;

namespace THANHTUNGO.PORTAL.Modules.MODULE.Controls
{
    public partial class StandardPermission : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private Module GetModule()
        {
            // Obtain Portal for this tab
            Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal)HttpContext.Current.Items["Portal"];

            // Obtain selected module data
            foreach (Module _module in portal.ActiveTab.Modules)
            {
                if (_module.ID == this.ModuleID)
                    return _module;
            }
            return null;
        }

        private static bool checkRole(string roleID, string roleString)
        {
            foreach (string r in roleString.Split(';'))
            {
                if (roleID.Trim() == r.Trim())
                {
                    return true;
                }
            }
            return false;
        }

        private void BindData()
        {
            object value = GetModule();
            if (value != null)
            {
                Module m = (Module)value;
                DataTable roles = Role.SelectAll().Tables[0];

                // View roles.
                chkViewRoles.Items.Clear();
                /*
                ListItem allViewItem = new ListItem();
                allViewItem.Text = "All Users";
                allViewItem.Value = "0";
                if (checkRole("0", m.AuthorizedViewRoles))
                {
                    allViewItem.Selected = true;
                }
                chkViewRoles.Items.Add(allViewItem);
                */
                foreach (DataRow row in roles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = row["Name"].ToString();
                    item.Value = row["ID"].ToString();
                    if (checkRole(item.Value, m.AuthorizedViewRoles))
                    {
                        item.Selected = true;
                    }
                    chkViewRoles.Items.Add(item);
                }


                // Add roles.
                chkAddRoles.Items.Clear();
                /*
                ListItem allAddItem = new ListItem();
                allAddItem.Text = "All Users";
                allAddItem.Value = "0";
                if (checkRole("0", m.AuthorizedAddRoles))
                {
                    allAddItem.Selected = true;
                }
                chkAddRoles.Items.Add(allAddItem);
                */
                foreach (DataRow row in roles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();
                    if (checkRole(item.Value, m.AuthorizedAddRoles))
                    {
                        item.Selected = true;
                    }
                    chkAddRoles.Items.Add(item);
                }


                // Edit roles.
                chkEditRoles.Items.Clear();
                /*
                ListItem allEditItem = new ListItem();
                allEditItem.Text = "All Users";
                allEditItem.Value = "0";
                if (checkRole("0", m.AuthorizedEditRoles))
                {
                    allEditItem.Selected = true;
                }
                chkEditRoles.Items.Add(allEditItem);
                */
                foreach (DataRow row in roles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();

                    if (checkRole(item.Value, m.AuthorizedEditRoles))
                    {
                        item.Selected = true;
                    }
                    chkEditRoles.Items.Add(item);
                }

                // Delete roles.
                chkDeleteRoles.Items.Clear();
                /*
                ListItem allDeleteItem = new ListItem();
                allDeleteItem.Text = "All Users";
                allDeleteItem.Value = "0";

                if (checkRole("0", m.AuthorizedDeleteRoles))
                {
                    allDeleteItem.Selected = true;
                }
                chkDeleteRoles.Items.Add(allDeleteItem);
                */
                foreach (DataRow row in roles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();

                    if (checkRole(item.Value, m.AuthorizedDeleteRoles))
                    {
                        item.Selected = true;
                    }
                    chkDeleteRoles.Items.Add(item);
                }


                // Property roles.
                chkPropertyRoles.Items.Clear();
                /*
                ListItem allPropertyItem = new ListItem();
                allPropertyItem.Text = "All Users";
                allPropertyItem.Value = "0";

                if (checkRole("0", m.AuthorizedPropertyRoles))
                {
                    allPropertyItem.Selected = true;
                }
                chkPropertyRoles.Items.Add(allPropertyItem);
                */
                foreach (DataRow row in roles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();

                    if (checkRole(item.Value, m.AuthorizedPropertyRoles))
                    {
                        item.Selected = true;
                    }
                    chkPropertyRoles.Items.Add(item);
                }


                // Manager roles.
                chkManagerRoles.Items.Clear();
                /*
                ListItem allPropertyItem = new ListItem();
                allPropertyItem.Text = "All Users";
                allPropertyItem.Value = "0";

                if (checkRole("0", m.AuthorizedPropertyRoles))
                {
                    allPropertyItem.Selected = true;
                }
                chkPropertyRoles.Items.Add(allPropertyItem);
                */
                foreach (DataRow row in roles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();

                    if (checkRole(item.Value, m.AuthorizedManagerRoles))
                    {
                        item.Selected = true;
                    }
                    chkManagerRoles.Items.Add(item);
                }
            }
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            object value = GetModule();
            if (value != null)
            {
                Module m = (Module)value;

                // View Roles.
                string viewRoles = "";
                foreach (ListItem item in chkViewRoles.Items)
                {
                    if (item.Selected)
                    {
                        viewRoles = viewRoles + item.Value + ";";
                    }
                }

                // Add Roles.
                string addRoles = "";
                foreach (ListItem item in chkAddRoles.Items)
                {
                    if (item.Selected)
                    {
                        addRoles = addRoles + item.Value + ";";
                    }
                }


                // Edit Roles.
                string editRoles = "";

                foreach (ListItem item in chkEditRoles.Items)
                {
                    if (item.Selected)
                    {
                        editRoles = editRoles + item.Value + ";";
                    }
                }

                // Delete Roles.
                string deleteRoles = "";
                foreach (ListItem item in chkDeleteRoles.Items)
                {
                    if (item.Selected)
                    {
                        deleteRoles = deleteRoles + item.Value + ";";
                    }
                }

                // Property Roles.
                string propertyRoles = "";
                foreach (ListItem item in chkPropertyRoles.Items)
                {
                    if (item.Selected)
                    {
                        propertyRoles = propertyRoles + item.Value + ";";
                    }
                }

                // Manager Roles.
                string managerRoles = "";
                foreach (ListItem item in chkManagerRoles.Items)
                {
                    if (item.Selected)
                    {
                        managerRoles = managerRoles + item.Value + ";";
                    }
                }

                // Update module.
                PortalConfiguration config = new PortalConfiguration();
                config.UpdateModule(m.ID, viewRoles, addRoles, editRoles, deleteRoles, propertyRoles, managerRoles);
            }

            // Redirect back to the portal home page.
            this.ReturnToReferrerPage();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            // Redirect back to the portal home page.
            this.ReturnToReferrerPage();
        }

        protected void btnPermission_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("~/MODULES/Module/ModulePermission.aspx?tabid={0}&mid={1}", this.TabID, this.ModuleID));
        }
    }
}